package com.lee.nio;

import java.nio.ByteBuffer;

/**
 * NIOByteBufferPutGet
 *
 * @author dralee
 * @date 2021-12-28 23:32:40
 **/
public class NIOByteBufferPutGet {
    public static void main(String[] args) {
        // 创建Buffer
        ByteBuffer buffer = ByteBuffer.allocate(64);
        // 类型化方式放入数据
        buffer.putInt(100);
        buffer.putLong(10);
        buffer.putChar('好');
        buffer.putShort((short) 12);

        // 取出
        buffer.flip();
        System.out.println();
        System.out.println(buffer.getInt());
        System.out.println(buffer.getLong());
        System.out.println(buffer.getChar());
        System.out.println(buffer.getShort());

        // 顺序不一致会数据异常或抛出异常

        // 取出
        buffer.flip();
        System.out.println("不一致顺序读取(异常)：");
        System.out.println(buffer.getLong());
        System.out.println(buffer.getInt());
        System.out.println(buffer.getShort());
        System.out.println(buffer.getChar());
    }
}
